using ServiceStack.Logging;
using System;
using System.Diagnostics;
using System.Threading;
namespace ServiceStack
{
	public static class FuncUtils
	{
		private static readonly ILog Log = LogManager.GetLogger(typeof(FuncUtils));
		public static bool TryExec(Action action)
		{
			try
			{
				action();
				return true;
			}
			catch (Exception ex)
			{
				FuncUtils.Log.Error(ex.Message, ex);
			}
			return false;
		}
		public static T TryExec<T>(Func<T> func)
		{
			return FuncUtils.TryExec<T>(func, default(T));
		}
		public static T TryExec<T>(Func<T> func, T defaultValue)
		{
			try
			{
				return func();
			}
			catch (Exception ex)
			{
				FuncUtils.Log.Error(ex.Message, ex);
			}
			return default(T);
		}
		public static void WaitWhile(Func<bool> condition, int millisecondTimeout, int millsecondPollPeriod = 10)
		{
			Stopwatch stopwatch = Stopwatch.StartNew();
			while (condition())
			{
				Thread.Sleep(millsecondPollPeriod);
				if (stopwatch.ElapsedMilliseconds > (long)millisecondTimeout)
				{
					throw new TimeoutException("Timed out waiting for condition function.");
				}
			}
		}
	}
}
